package com.example.sofa_model;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import com.blankj.utilcode.util.LogUtils;
import com.bw.ijk.widget.IjkView;
import com.example.common.mvvm.view.BaseFragment;
import com.example.sofa_model.adapter.ImageAdapter;
import com.example.sofa_model.adapter.TextAdapter;
import com.example.sofa_model.adapter.VideoAdapter;
import com.example.sofa_model.databinding.FragmentSofaTabBinding;
import com.example.sofa_model.entity.DataDTO;
import com.example.sofa_model.entity.DataDTOX;

import java.util.List;
import java.util.Map;

import tv.danmaku.ijk.media.player.IjkMediaPlayer;

public class SofaTabFragment extends BaseFragment<FragmentSofaTabBinding, SofaViewModel> {


    private String target;
    private RecyclerView sofaTabRecycler;
    private List<DataDTO> dataDTOList;

    @Override
    public View bindLayout() {
        return v.getRoot();
    }

    @Override
    public ViewBinding initBind() {
        return FragmentSofaTabBinding.inflate(inflater);
    }

    @Override
    public void initView() {
        sofaTabRecycler = v.sofaTabRecycler;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        LogUtils.e("销毁");
    }

    @Override
    public void initData() {
        IjkMediaPlayer.loadLibrariesOnce(null);
        IjkMediaPlayer.native_profileBegin("libijkplayer.so");
    }

    @Override
    public void onResume() {
        super.onResume();
        Bundle arguments = getArguments();
        target = arguments.getString("target");
        if (target.equals("image")) {
            vm.initImage();
        } else if (target.equals("video")) {
            vm.initVideo();
        } else if (target.equals("text")) {
            vm.initText();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtils.e("销毁sofatabfragment");
    }

    @Override
    public void onPause() {
        super.onPause();
        LogUtils.e("onPause sofatabfragment");
    }

    @Override
    public void receiveData(Map<String, Object> data) {
        DataDTOX dataDTOX = (DataDTOX) data.get("VIDEODATA");
        dataDTOList = dataDTOX.data;
        if (target.equals("image")) {

            ImageAdapter adapter = (ImageAdapter) sofaTabRecycler.getAdapter();
            if (adapter == null) {
                adapter = new ImageAdapter();
                sofaTabRecycler.setLayoutManager(new LinearLayoutManager(sofaTabRecycler.getContext(), RecyclerView.VERTICAL, false));
                sofaTabRecycler.setAdapter(adapter);
                adapter.setNewInstance(dataDTOList);
            } else {
                adapter.setNewInstance(dataDTOList);
            }


        } else if (target.equals("text")) {
            TextAdapter adapter = (TextAdapter) sofaTabRecycler.getAdapter();
            if (adapter == null) {
                adapter = new TextAdapter();
                sofaTabRecycler.setLayoutManager(new LinearLayoutManager(sofaTabRecycler.getContext(), RecyclerView.VERTICAL, false));
                sofaTabRecycler.setAdapter(adapter);
                adapter.setNewInstance(dataDTOList);
            } else {
                adapter.setNewInstance(dataDTOList);
            }
        } else if (target.equals("video")) {


            VideoAdapter adapter = (VideoAdapter) sofaTabRecycler.getAdapter();
            if (adapter == null) {
                adapter = new VideoAdapter();
                sofaTabRecycler.setLayoutManager(new LinearLayoutManager(sofaTabRecycler.getContext(), RecyclerView.VERTICAL, false));
                sofaTabRecycler.setAdapter(adapter);
                sofaTabRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                        super.onScrollStateChanged(recyclerView, newState);
                        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                            //停止状态
                            LinearLayoutManager recylerManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                            int firstPosition = recylerManager.findFirstCompletelyVisibleItemPosition();
                            LogUtils.e("firstPosition"+firstPosition);
                            View view = recylerManager.findViewByPosition(firstPosition);
                            if (view != null) {
                                IjkView ijkView = view.findViewById(R.id.feed_video);
                                String url = dataDTOList.get(firstPosition).url;
                                ijkView.setPlayUrl(url);
                            }

                        }  else if (newState == RecyclerView.SCROLL_STATE_SETTLING) {
                            //手指抛动 - > recyclerview 惯性滑动动作
                            //全部停止播放
                            LinearLayoutManager recylerManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                            for (int i = 0;i < recyclerView.getAdapter().getItemCount();i++){
                                View view = recylerManager.findViewByPosition(i);
                                if (view!=null){
                                    IjkView ijkView = view.findViewById(R.id.feed_video);
                                    if (ijkView.isPlaying())
                                        ijkView.pausePlay();
                                }
                            }
                        }

                    }

                    @Override
                    public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                        super.onScrolled(recyclerView, dx, dy);
                    }
                });
                adapter.setNewInstance(dataDTOList);
            } else {
                adapter.setNewInstance(dataDTOList);
            }
        }
    }
}
